<?php

class OthersController extends Common {

    private $startup;
    private $timemoney;

    public function __construct() {
        parent::__construct();
        $this->startup = $this->model('startup');
        $this->timemoney = $this->model('timemoney');
        $this->ad_price = $this->model('ad_price');
        $this->mail = $this->model('mail');
    }

    /**
     * 设置引导页广告
     */
    public function startupAction()
    {
        $startupImg = $this->startup->getStartup(); //获取引导页图片
        if($this->post('submit')) {
            $file = $_FILES['file'];
            //得到文件名称
            $name = $file['name'];
            if(empty($file) || empty($name)){
                $this->memberMsg('上传失败,请选择图片', url('admin/others/startup'));
            }
            $type = strtolower(substr($name,strrpos($name,'.')+1)); //得到文件类型，并且都转化成小写
            $allow_type = array('jpg','jpeg','gif','png'); //定义允许上传的类型
            //判断文件类型是否被允许上传
            if(!in_array($type, $allow_type)){
                //如果不被允许，则直接停止程序运行
                $this->memberMsg('上传失败,图片类型有误', url('admin/others/startup'));
            }
            //判断是否是通过HTTP POST上传的
            if(!is_uploaded_file($file['tmp_name'])){
                //如果不是通过HTTP POST上传的
                $this->memberMsg('上传失败,提交类型有误', url('admin/others/startup'));
            }
            $upload_path = FCPATH."/uploadfiles/image/startup/"; //上传文件的存放路径
            //开始移动文件到相应的文件夹
            if(!move_uploaded_file($file['tmp_name'],$upload_path.$file['name'])){
                $this->memberMsg('上传失败', url('admin/others/startup'));
            }

            $upResult = $this->startup->updateStartup($file['name']);  //更新引导页面图片
            if(empty($upResult)){
                $this->memberMsg('保存失败', url('admin/others/startup'));
            }
            $this->adminMsg('上传成功', url('admin/others/startup'), 3, 1, 1);
        }
        $this->view->assign(array(
            'img' => 'http://'.$_SERVER['HTTP_HOST'].'/ACarAdvertistingCms/uploadfiles/image/startup/'.$startupImg->img,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/others/startup');  //这里是对视图文件的加载
    }

    /**
     * 收费金额管理
     */
    public function timesAction()
    {
        $timesInfo = $this->timemoney->getTimemoney();
        if($this->post('submit')){
            $post = $_POST;
            unset($post['submit']);
            $param = [];
            $i=0;
            foreach($post as $times){
                if(empty($times)){
                    $this->memberMsg('更新失败', url('admin/others/times'));
                    return ;
                }
                $param[$i] = intval($times);
                $i++;
            }
            $isUpdate = $this->timemoney->updateTimemoney(json_encode($param));    //更新倍数
            if(empty($isUpdate)){
                $this->memberMsg('更新失败', url('admin/others/times'));
            }
            $this->adminMsg('更新成功', url('admin/others/times'), 3, 1, 1);
        }
        $this->view->assign(array(
            'data' => $timesInfo->times?json_decode($timesInfo->times,true):[],
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/others/times');  //这里是对视图文件的加载
    }

    /**
     * 收费金额管理
     */
    public function setpriceAction()
    {
        $priceInfo = $this->ad_price->getPrice();      //获取引导页图片
        if($this->post('submit')){
            if(empty($_POST['price'])){
                $this->adminMsg('价格不能为空', url('admin/others/setprice'), 3, 1, 0);
            }
            $isUpdate = $this->ad_price->updatePrice(floatval($_POST['price']));    //更新价格
            if(empty($isUpdate)){
                $this->adminMsg('更新失败', url('admin/others/setprice'), 3, 1, 0);
            }
            $this->adminMsg('更新成功', url('admin/others/setprice'), 3, 1, 1);
        }
        $this->view->assign(array(
            'price' => $priceInfo->price?:0,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/others/price');  //这里是对视图文件的加载
    }


    /**
     * 列表
     */
    public function emailAction() {
        $userid = $this->session->get('user_id');   //从session中获取用户id
        if(empty($userid)){
            echo "请重新登录";exit;
        }
        $page     = $this->get('page') ? $this->get('page') : 1;
        $pagesize = 10;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $where = '';
        $model = $this->model('mail');
        $total = $model->count('mail', 'id', $where);
        $urlparam = array(
            'page' => '{page}',
        );
        $data = $model->page_limit($page, $pagesize)->order(array('id DESC'))/*->where($where)*/->select();
        $pagelist = $pagelist->total($total)->url(url('admin/others/email', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'page' => $page,
            'data' => $data,
            'pagelist'=> $pagelist,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/others/mail_list');  //这里是对视图文件的加载
    }


    /**
     * 列表
     */
    public function feedbackAction() {
        $userid = $this->session->get('user_id');   //从session中获取用户id
        if(empty($userid)){
            echo "请重新登录";exit;
        }
        $page     = $this->get('page') ? $this->get('page') : 1;
        $pagesize = 10;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $where = '';
        $model = $this->model('feedback');
        $total = $model->count('feedback', 'id', $where);
        $urlparam = array(
            'page' => '{page}',
        );
        $data = $model->page_limit($page, $pagesize)->order(array('id DESC'))/*->where($where)*/->select();
        $pagelist = $pagelist->total($total)->url(url('admin/others/feedback', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'page' => $page,
            'data' => $data,
            'pagelist'=> $pagelist,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/others/feedback');  //这里是对视图文件的加载
    }

    public function sfAction(){
        $username=$_POST[username];   //获取视图文件传过来的表单的值  表单我用post传值
        // $model = $this->model('member');  //实例化model 这里的两行是用fine的原有的写的，但是我感觉这样不灵活我下面又用php的写了因为我感觉还是php写的顺手，大家自己感觉什么好就用什么吧
        // $data = $model->getAll('username=?', "$username"); //用的fine的getall函数
        $sql="select * from fn_member where username='$username' ";   //sql语句  这里是php的
        $row=mysql_query($sql);  //php执行sql
        $data=mysql_fetch_assoc($row);  //获取sql语句得到的值
        $this->view->assign('data',$data); //assign()对视图变量完成赋值
        $this->view->display('sf'); //加载视图文件
    }

}
